#include "widget.h"
#include "ui_widget.h"

Q_DECLARE_METATYPE(QVector<QPoint>);

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    qRegisterMetaType<QVector<QPoint>>("QVector<QPoint>");

    this->setWindowTitle(QStringLiteral("贝塞尔曲线平滑测试"));
    this->setFixedSize(1263, 1075);

    m_thread = new QThread;
    m_worker = new CWorker;
    m_worker->moveToThread(m_thread);
    connect(m_thread, &QThread::started, m_worker, &CWorker::slotCreateObjects);

    connect(m_worker, &CWorker::signalSendTwoOrderBezierPoint,   ui->widget_displayWidget, &CDisplayWidget::slotRecvTwoOrderBezierPoints);
    connect(m_worker, &CWorker::signalSendThreeOrderBezierPoint, ui->widget_displayWidget, &CDisplayWidget::slotRecvThreeOrderBezierPoints);
    connect(this,     &Widget::signalSendStartFit,               m_worker,                 &CWorker::slotFitting);
    connect(m_worker, &CWorker::signalTwoOrderRollLine,          ui->widget_displayWidget, &CDisplayWidget::slotRecvTwoBezierRollLine);
    connect(m_worker, &CWorker::signalThreeOrderRollLine,        ui->widget_displayWidget, &CDisplayWidget::slotRecvThreeBezierRollLine);
    m_thread->start();

    bindSlotAndSignals();

    ui->checkBox_paintLine->setChecked(true);
    ui->checkBox_paintBezierCurve->setChecked(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::bindSlotAndSignals()
{
    connect(this, &Widget::signalClearWidgetPoints, ui->widget_displayWidget, &CDisplayWidget::slotClearAll);
    connect(this, &Widget::signalSendPaintLineFlag, ui->widget_displayWidget, &CDisplayWidget::slotRecvPaintOriginLineFlag);
    connect(this, &Widget::signalSendPaintCurveFlag, ui->widget_displayWidget, &CDisplayWidget::slotRecvPaintCurveFlag);

    connect(ui->widget_displayWidget, &CDisplayWidget::signalSendMouseLeftClickedPoint, this, &Widget::slotGetBezierDispersedPoints);
    connect(ui->widget_displayWidget, &CDisplayWidget::signalClearAll, this, &Widget::slotClearAll);
}

void Widget::slotGetBezierDispersedPoints(QPoint point)
{
    m_bezier_dispersed_points.push_back(point);
}

void Widget::slotClearAll()
{
    m_bezier_dispersed_points.clear();
}


void Widget::on_pushButton_startFit_clicked()
{
    int order = ui->comboBox_fitOrder->currentIndex();
    emit signalSendStartFit(order + 1, m_bezier_dispersed_points);
}


void Widget::on_pushButton_clearPoints_clicked()
{
    m_bezier_dispersed_points.clear();
    emit signalClearWidgetPoints();
}


void Widget::on_checkBox_paintLine_stateChanged(int arg1)
{
    if (arg1)
    {
        emit signalSendPaintLineFlag(true);
    }
    else
    {
        emit signalSendPaintLineFlag(false);
    }
}


void Widget::on_checkBox_paintBezierCurve_stateChanged(int arg1)
{
    if (arg1)
    {
        emit signalSendPaintCurveFlag(true);
    }
    else
    {
        emit signalSendPaintCurveFlag(false);
    }
}

